/*
 * @Author: zzh
 * @Date: 2021-04-17 14:49:35
 * @LastEditTime: 2021-06-07 16:50:48
 * @LastEditors: Please set LastEditors
 * @Description: 增加用户登出模块
 * @FilePath: \egg-demo\app\service\auth.js
 */
'use strict';

const { Service } = require('egg');

class AuthService extends Service {

  /* 用户登录成功； 返回token */
  async login() {
    const { ctx, app } = this;
    const { username, password } = await ctx.request.body;
    try { 
      const userInfo = { name: username, uuid: 'q1damjkinqiwj0and109' }; //  暂时，需要查询数据库
      if(userInfo) {
        const token = app.jwt.sign({ username: userInfo.name, uuid: userInfo.uuid }, app.config.jwt.secret );
        return token;
      } else {
        ctx.helper.renderFail( ctx, { message: '用户名或者密码错误' } );
      }
    } catch (err) {
      ctx.helper.renderFail( ctx, { message: err, data: err })
    }
  }

  /* 返回登录用户信息 */
  async checkLogin() {
    const { ctx } = this;
    const loginUser = ctx.state.user;
    return loginUser;
  }

  /* 用户登出： 主动释放token */
  async logout() {
    const { ctx, app } = this;
    
  }
}

module.exports = AuthService;